<?php
namespace app\api\controller\v2\controller\userInfo;

use app\api\controller\v2\model\userInfo\UserInfo as Yu;
use think\Controller;

use app\api\controller\Api;
//use think\Model;
use think\Db;
use think\Image;
use JPush\Client as JPush;
use think\Request;
//use think\response\Redirect;

/**
 *
 */
class userInfo extends api
{
    //显示个人信息
    public function show_userinfo($user_id){
        $result =(new Yu())->getShowUserInfo($user_id);
        $result[0]['code'] = 200;
        $result[0]['access_token'] = Request::instance()->param('access_token');
        return $this->returnmsg(200,'个人信息',$result);

    }

//显示编辑用户资料
    public function edit_user_info($user_id){
        $result = Yu::getEditUserInfo($user_id);

        return $this->returnmsg(200,'用户资料',$result);
    }

    //上传头像
    public function change_user_icon($user_id)
    {
        if($this->request->isPost()){
            // 获取上传文件
            $files = Request::instance()->file('user_icon');
            // 移动到框架应用根目录/public/uploads/ 目录下
            // $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            $info = $files->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'icon');

            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $path = '/uploads/icon/' . $info->getSaveName();
                $path = str_replace('\\', '/', $path);
                // $icon_path = str_replace('\\', '/', $path);
                $res = Db::query("update user_info set user_icon = '$path' where user_id = '$user_id'");
                // $image
                $image = Image::open(request()->file('user_icon'));
                // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                $image->thumb(105, 105, Image::THUMB_CENTER)->save(ROOT_PATH . 'public' . $path);

                if (empty($res)) {
                    return  $this->returnmsg(200, '头像上传成功!', 'icon_url: '.$path);
                } else {
                    return  $this->returnmsg(400, '上传头像失败!');
                }
            }else{
                // 上传失败获取错误信息
                echo $files->getError();
            }
        }
    }

//上传用户资料
    public function userInfo_upload($user_id,$user_name,$user_gender,$user_age,$pet_type,$user_type)
    {

        switch ($user_gender) {
            case '男':
                $user_gender = 0;
                break;
            case '女':
                $user_gender = 1;
                break;
            case '保密':
                $user_gender = 2;
                break;
        }

        switch ($pet_type) {
            case '狗':
                $pet_type = 0;
                break;
            case '猫':
                $pet_type = 1;
                break;
            case '两者都是':
                $pet_type = 2;
                break;
            case '其它':
                $pet_type = 3;
                break;
        }

        switch ($user_type) {
            case '吸宠者':
                $user_type = 0;
                break;
            case '铲屎官':
                $user_type = 1;
                break;
        }
    echo 1;
        $result = Db::query("select user_name from user_info where user_id = '$user_id' ");
    echo 2;
        if (!empty($result)) {
            return $this->returnmsg(401,'Fail,User name exist');
        }else{
            // 获取上传文件
            $files = Request::instance()->file('user_icon');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $files->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'icon');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $path = '/uploads/icon/' . $info->getSaveName();
                $photo = $info->getFilename();
                $small_path = ROOT_PATH . 'public' . DS. 'uploads'.DS.'small_user_icon'.DS.$photo;
                //原大图路径,当时我获取的时候费了不少时间  .DS.date("m-d")
                // $small_path = 'https://cloudpetor.com/api/public/uploads/small_user_icon'.DS.$photo;
                $path = str_replace('\\', '/', $path);
                $small_path = str_replace('\\', '/', $small_path);
                // $image
                $image = Image::open(request()->file('user_icon'));
                // 按照原图的比例生成一个最大为72*72的缩略图并保存为thumb.png
                $image->thumb(72, 72, Image::THUMB_CENTER)->save($small_path);
                $follow_count = Db::table('ym_user_follow')->where('user_id',$user_id)->count('user_id');
                $fans_count = Db::table('ym_user_follow')->where('follow_id',$user_id)->count('follow_id');
                $len = strlen(ROOT_PATH. 'public');
                $small_path = substr_replace($small_path,'',0,$len);
                echo 3;
                Db::query("insert into user_info 
                                values('$user_id','$user_name','$user_gender','$user_age',default,'$user_type','$follow_count','$fans_count','$pet_type',default,'$path','$small_path',default,default)");
                echo 4;
                return $this->returnmsg(200,"用户资料添加成功");
            }
        }
    }

    //判断用户类型
    public function judgement_user_type($user_id){
        $result = Yu::getJudgementUserType($user_id);

        if ($result[0]['user_type'] = 0) {
            return $this->returnmsg(200,"吸宠者");
        }else{
            return $this->returnmsg(200,"铲屎官");
        }
    }

    //修改用户背景图片
    public function change_background_img($user_id){
        // 获取上传文件
        $files = Request::instance()->file('background_img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if (!empty($files)) {
            $info = $files->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'background_img');

            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $path = '/uploads/background_img/' . $info->getSaveName();
                $path = str_replace('\\', '/', $path);
                Db::query("update user_info set background_img = '$path' where user_id = '$user_id'");
            }
        }
    }

    //显示被关注的人(粉丝)
    public function show_fans($user_id,$other_user_id,$pageno){

        $arr = Yu::getShowFans($user_id,$other_user_id,$pageno);
        return $this->returnmsg(200,'粉丝',$arr);
    }

    //用户关注的人
    public function show_follows($user_id,$other_user_id,$pageno){

        $arr = Yu::getShowFollows($user_id,$other_user_id,$pageno);
        return $this->returnmsg(200,'关注的人',$arr);
    }

    //修改密码
    public function change_psw($user_id,$password,$confirm_password,$new_password){
        $psw = Db::query("select password from ym_user where id = '$user_id'");
        if (md5($password) == $psw[0]['password'] ) {

            if ($new_password == $confirm_password) {
                Db::query("update ym_user set password = 'md5($new_password)' where id = '$user_id'");
                return $this->returnmsg(200,"修改密码成功");
            }else{
                return $this->returnmsg(401,"两次输入密码不一致");
            }
        }else{
            return $this->returnmsg(402,"旧密码错误");
        }
    }


//    修改用户资料
//    public function test($user_id){
//        $small_path = ROOT_PATH . 'public' . DS. 'uploads'.DS.'small_user_icon';
//        print($small_path);
//    }
//
    public function change_userinfo($user_id,$user_name,$user_gender,$user_age,$user_desc,$user_type){

        switch ($user_gender) {
            case '男':
                $user_gender = 0;
                break;
            case '女':
                $user_gender = 1;
                break;
            case '保密':
                $user_gender = 2;
                break;
        }
        switch ($user_type) {
            case '吸宠者':
                $user_type = 0;
                break;
            case '铲屎官':
                $user_type = 1;
                break;
        }

        $file = Request::instance()->file('user_icon');
        if ($file) {

            $info = $file->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'icon');
            if ($info) {
                $path = '/uploads/icon/' . $info->getSaveName();
                $photo = $info->getFilename();
                $small_path = ROOT_PATH . 'public' . DS. 'uploads'.DS.'small_user_icon'.DS.$photo;
                //print_r($small_path);
                $path = str_replace('\\', '/', $path);
                $small_path = str_replace('\\', '/', $small_path);
                // $image
                $image = Image::open(request()->file('user_icon'));
                // 按照原图的比例生成一个最大为72*72的缩略图并保存为thumb.png
                $image->thumb(72, 72, Image::THUMB_CENTER)->save($small_path);

                $len = strlen(ROOT_PATH. 'public');

                $small_path = substr_replace($small_path,'',0,$len);

                Db::query("update user_info set user_name = '$user_name',user_gender = '$user_gender',user_age = '$user_age',user_type = '$user_type',user_desc = '$user_desc',user_icon = '$path',small_user_icon = '$small_path' where user_id = '$user_id'");
                $this->change_background_img($user_id);
                //return $this->returnmsg(200,"资料修改成功22");
            }
        }else{
            Db::query("update user_info set user_name = '$user_name',user_gender = '$user_gender',user_age = '$user_age',user_type = '$user_type',user_desc = '$user_desc' where user_id = '$user_id'");
            $this->change_background_img($user_id);
        }
        return $this->returnmsg(200,"资料修改成功");
    }









}